package com.example.event_resource_java.commons.util;


import com.example.event_resource_java.commons.enums.ApiEnumError;
import lombok.Builder;

import java.io.Serializable;

//@Builder
public class ApiResult<T> implements Serializable {
    public static final String FAIL_CODE = "0";        //成功码
    public static final String SUC_CODE = "1";        //失败码
    public static final String ERROR_CODE = "2";    //错误码
    public static final String SUC_MESSAGE = "Operate successfully";    //成功信息
    public static final String FAIL_MESSAGE = "Operation failure";        //失败信息
    public static final String ERROR_MESSAGE = "System Error";            //错误信息
    public static final String NOACCESS_MESSAGE = "No permission to access this page.";        //无权操作
    // 响应业务状态 0 成功， 1失败
    private Integer code;
    // 响应消息
    private String msg;

    private boolean success;

    public boolean getSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    // 响应中的数据
    private T data;

    public static <T> ApiResult<T> build(Integer status, String msg, T data) {
        return new ApiResult<T>(status, msg, data);
    }
    public static <T> ApiResult<T> build(Integer status, String msg) {
        return new ApiResult<T>(status, msg);
    }

    public static <T> ApiResult<T> lack(String msg) {
        return new ApiResult<T>(ApiEnumError.lackParameter.getCode(),ApiEnumError.lackParameter.getMsg() + msg);
    }

    public static <T> ApiResult<T> format(String msg) {
        return new ApiResult<T>(ApiEnumError.formatFail.getCode(),msg + ApiEnumError.formatFail.getMsg());
    }

    public static <T> ApiResult<T> success(T data) {
        return new ApiResult<T>(data);
    }

    public static <T> ApiResult<T> success() {
        return new ApiResult<T>(ApiEnumError.success.getCode(), ApiEnumError.success.getMsg(), null);
    }
    public static <T> ApiResult<T> success(String msg) {
        return new ApiResult<T>(ApiEnumError.success.getCode(), msg, null);
    }
    public static <T> ApiResult<T> success(String msg, T data) {
        return new ApiResult<T>(ApiEnumError.success.getCode(), msg, data);
    }

    public static <T> ApiResult<T> fail() {
        return new ApiResult<T>(ApiEnumError.unKnowFail.getCode(), ApiEnumError.unKnowFail.getMsg(), null);
    }
    public static <T> ApiResult<T> fail(String msg) {
        return new ApiResult<T>(ApiEnumError.unKnowFail.getCode(), msg, null);
    }
    public static <T> ApiResult<T> failData(T Data) {
        return new ApiResult<T>(ApiEnumError.unKnowFail.getCode(), ApiEnumError.unKnowFail.getMsg(), Data);
    }


    public static <T> ApiResult<T> fail(Integer code,String msg) {
        return new ApiResult<T>(code, msg, null);
    }

    public static <T> ApiResult<T> invalid() {
        return new ApiResult<T>(ApiEnumError.notAuthorization.getCode(),ApiEnumError.notAuthorization.getMsg());
    }

    public static <T> ApiResult<T> build(String codeStr) {
        int code = Integer.parseInt(codeStr);
        return new ApiResult<>(code,ApiEnumError.getMsgByCode(code));
    }


    public ApiResult() {
    }

    public ApiResult(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public ApiResult(T data) {
        this.code = ApiEnumError.success.getCode();
        this.msg = ApiEnumError.success.getMsg();
        this.data = data;
    }

    public ApiResult(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
